package com.example.dilidili_ting.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.dilidili_ting.R;
import com.example.dilidili_ting.activity.MovieDetailsActivityTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.LikeBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.example.dilidili_ting.view.LikeViewTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LikeAdapterTing extends RecyclerView.Adapter<LikeAdapterTing.LikeHolder>  {

    private Activity contextTing;
    private List<MovieBeanTing> dataTing;
    private DataBeanTing<MovieBeanTing> r;
    private LikeViewTing likeViewTing;

    //    构造函数
    public LikeAdapterTing(Activity context, List<MovieBeanTing> data) {
        this.contextTing=context;
        this.dataTing=data;
    }

    @NonNull
    @Override
    public LikeAdapterTing.LikeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(contextTing).inflate(R.layout.like_list_item_ting, parent, false);
        return new LikeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LikeAdapterTing.LikeHolder holder, int position) {
        Integer id = dataTing.get(position).getMovieId();
        holder.tvScMovienameTing.setText(dataTing.get(position).movieName);
        holder.scCastTing.setText(dataTing.get(position).cast);
        holder.scDirectorTing.setText(dataTing.get(position).director);
        RequestOptions options = new RequestOptions().error(R.drawable.k8).bitmapTransform(new RoundedCorners( 20 ));//图片圆角为30
        Glide.with(holder.ivScMovieimgTing.getContext()).load(dataTing.get(position).movieCover) //图片地址
                .apply(options)
                .into(holder.ivScMovieimgTing);
        holder.btnScTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除弹窗
                new SweetAlertDialog(contextTing, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("你确定要取消收藏？")
                        .setConfirmText("确定")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                sDialog
                                        .setTitleText("取消成功")
                                        .setConfirmText("OK")
                                        .setConfirmClickListener(null)
                                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);

                                contextTing.runOnUiThread(new Runnable(){
                                    @Override
                                    public void run() {
                                        Timer timer=new Timer();
                                        TimerTask task=new TimerTask() {
                                            @Override
                                            public void run() {
                                                btn_click(position,id);
                                                sDialog.dismiss();
                                            }
                                        };
                                        timer.schedule(task,1200);
                                    }
                                });
                            }
                        })
                        .show();
            }
        });
//        点击跳转到详情页
        holder.scLlTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(contextTing, MovieDetailsActivityTing.class);
                i.putExtra("movieName",dataTing.get(position).movieName);
                contextTing.startActivity(i);
            }
        });
    }

    //    取消收藏按钮
    private void btn_click(int position, int movieId) {
        JsonObject jo = new JsonObject();
        jo.addProperty("userId", SharedPreferencesUtilsTing.readLoginUserID(contextTing));
        jo.addProperty("movieId",movieId);
        OkhttpUtilsTing.isPOST("favorite/cancelFavorite", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                contextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dataTing.remove(position);
                        LikeAdapterTing.this.notifyDataSetChanged();
                    }
                });
            }
        });
    }

//    //根据id查找电影
//    private void setData(@NonNull LikeAdapterTing.LikeHolder holder,Integer id){
//        JsonObject jo = new JsonObject();
//        jo.addProperty("movieId",id);
//        OkhttpUtilsTing.isPOST("movie/getMovieById", true, jo, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {}
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String res = response.body().string();
//                Gson gson = new Gson();
//                r = gson.fromJson(res, new TypeToken<DataBeanTing<MovieBeanTing>>(){}.getType());
//                contextTing.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        System.out.println(res);
//                        holder.tvScMovienameTing.setText(r.data.movieName);
//                        holder.scCastTing.setText("周冬雨");
//                    }
//                });
//            }
//        });
////        notifyDataSetChanged(); //动态更新ListView
//    }

    @Override
    public int getItemCount() {
        return dataTing.size();
    }

    class LikeHolder extends RecyclerView.ViewHolder {

        private LinearLayout scLlTing;
        private ImageView ivScMovieimgTing;
        private TextView tvScMovienameTing;
        private TextView scDirectorTing;
        private TextView scCastTing;
        private Button btnScTing;

        public LikeHolder(View itemView) {
            super(itemView);
            scLlTing = (LinearLayout) itemView.findViewById(R.id.sc_ll_ting);
            ivScMovieimgTing = (ImageView) itemView.findViewById(R.id.iv_sc_movieimg_ting);
            tvScMovienameTing = (TextView) itemView.findViewById(R.id.tv_sc_moviename_ting);
            scDirectorTing = (TextView) itemView.findViewById(R.id.sc_director_ting);
            scCastTing = (TextView) itemView.findViewById(R.id.sc_cast_ting);
            btnScTing = (Button) itemView.findViewById(R.id.btn_sc_ting);
        }
    }

}
